/**
 * 
 */
package com.hjx.service.impl;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import com.hjx.model.Page;
import com.hjx.service.IGenericService;

/**
 * @author huangjixin
 * 
 */

public abstract class AbstractGenericService<T extends Serializable, PK extends Serializable> 
				implements IGenericService<T, PK>{

	@Override
	public int deleteByPrimaryKey(PK id) {
		return 0;
	}

	@Override
	public int insert(T record) {
		Method methlist[] = record.getClass().getDeclaredMethods();
		Method gId = null;
		Method gParentid = null;
		Method sId = null;
		Method sParentid = null;
		for (Method method : methlist) {
			if(method.getName() == "getId"){
				gId = method;
			}
			if(method.getName() == "getParentid"){
				gParentid = method;
			}
			if(method.getName() == "setId"){
				sId = method;
			}
			if(method.getName() == "setParentid"){
				sParentid = method;
			}
		}
		
		
		try {
			if(gId != null){
				Integer id = (Integer)gId.invoke(record);
				if(id==null){
					return 0;
				}
				if (id == 0) {
					try {
						Object[] idArguments = new Object[] {null}; 
						sId.invoke(record, idArguments);
//						record.getClass().getMethod("setId").invoke(record, idArguments);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
			
			if(gParentid != null){
				Integer parentId = (Integer)gParentid.invoke(record);
				if(parentId==null){
					return 0;
				}
				if(parentId == 0){
					try {
						Object[] parentIdArguments = new Object[] {null}; 
						sParentid.invoke(record, parentIdArguments);
//						record.getClass().getMethod("setParentId").invoke(record, parentIdArguments);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int insertSelective(T record) {
		Method methlist[] = record.getClass().getDeclaredMethods();
		Method gId = null;
		Method gParentid = null;
		Method sId = null;
		Method sParentid = null;
		for (Method method : methlist) {
			if(method.getName() == "getId"){
				gId = method;
			}
			if(method.getName() == "getParentid"){
				gParentid = method;
			}
			if(method.getName() == "setId"){
				sId = method;
			}
			if(method.getName() == "setParentid"){
				sParentid = method;
			}
		}
		
		
		try {
			if(gId != null){
				int id = (Integer)gId.invoke(record);
				if (id == 0) {
					try {
						Object[] idArguments = new Object[] {null}; 
						sId.invoke(record, idArguments);
//						record.getClass().getMethod("setId").invoke(record, idArguments);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
			
			if(gParentid != null){
				int parentId = (Integer)gParentid.invoke(record);
				if(parentId == 0){
					try {
						Object[] parentIdArguments = new Object[] {null}; 
						sParentid.invoke(record, parentIdArguments);
//						record.getClass().getMethod("setParentId").invoke(record, parentIdArguments);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<T> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page selectByPage(Page page) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public T selectByPrimaryKey(PK id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(T record) {
		Method methlist[] = record.getClass().getDeclaredMethods();
		Method gId = null;
		Method gParentid = null;
		Method sId = null;
		Method sParentid = null;
		for (Method method : methlist) {
			if(method.getName() == "getId"){
				gId = method;
			}
			if(method.getName() == "getParentid"){
				gParentid = method;
			}
			if(method.getName() == "setId"){
				sId = method;
			}
			if(method.getName() == "setParentid"){
				sParentid = method;
			}
		}
		
		
		try {
			if(gId != null){
				int id = (Integer)gId.invoke(record);
				if (id == 0) {
					try {
						Object[] idArguments = new Object[] {null}; 
						sId.invoke(record, idArguments);
//						record.getClass().getMethod("setId").invoke(record, idArguments);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
			
			if(gParentid != null){
				int parentId = (Integer)gParentid.invoke(record);
				if(parentId == 0){
					try {
						Object[] parentIdArguments = new Object[] {null}; 
						sParentid.invoke(record, parentIdArguments);
//						record.getClass().getMethod("setParentId").invoke(record, parentIdArguments);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int updateByPrimaryKey(T record) {
		Method methlist[] = record.getClass().getDeclaredMethods();
		Method gId = null;
		Method gParentid = null;
		Method sId = null;
		Method sParentid = null;
		for (Method method : methlist) {
			if(method.getName() == "getId"){
				gId = method;
			}
			if(method.getName() == "getParentid"){
				gParentid = method;
			}
			if(method.getName() == "setId"){
				sId = method;
			}
			if(method.getName() == "setParentid"){
				sParentid = method;
			}
		}
		
		
		try {
			if(gId != null){
				int id = (Integer)gId.invoke(record);
				if (id == 0) {
					try {
						Object[] idArguments = new Object[] {null}; 
						sId.invoke(record, idArguments);
//						record.getClass().getMethod("setId").invoke(record, idArguments);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
			
			if(gParentid != null){
				int parentId = (Integer)gParentid.invoke(record);
				if(parentId == 0){
					try {
						Object[] parentIdArguments = new Object[] {null}; 
						sParentid.invoke(record, parentIdArguments);
//						record.getClass().getMethod("setParentId").invoke(record, parentIdArguments);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}
